package com.small.aop;

import java.lang.reflect.Method;
import com.small.proxy.Proxy;
import com.small.proxy.ProxyChain;

/**
 * Aop切面代理类
 * @author 乐乐
 *
 */
public abstract class AspectProxy implements Proxy {
  
  public Object doProxy(ProxyChain chain) throws Throwable {
    Object result = null;
    begin();
    Method targetMethod = chain.getTargetMethod();
    Object[] methodParams = chain.getMethodParams();
    try {
      before(targetMethod, methodParams);
      result = chain.doChain();
      after(targetMethod, methodParams);
    } catch (Exception e) {
      error(targetMethod, methodParams, e);
    }finally {
      end();
    }
    
    return result;
  }

  /**
   * 开始增强
   */
  public void begin() {
  }
  
  /**
   * 前置增强
   */
  public void before(Method method, Object[] params) throws Throwable {
  }

  /**
   * 后置增强
   */
  public void after(Method method, Object[] params) throws Throwable {
  }

  /**
   * 异常增强
   */
  public void error(Method method, Object[] params, Throwable e) {
  }

  /**
   * 最终增强
   */
  public void end() {
  }
  
}
